<?PHP

class DB
{
	/// (INT - Resource) Mysql - connection ID
	private static $my_connect=array();
	/// (String) Last SQL- QueryString
	private $lastSQL;
	/// (Long)Last Insert ID
	private $insertId;
	/// (MysqlResult)
	private $result;

	/// (Long) Mysql-Error ID
	private $error;

	/// verbindugsparameter
	private $parameterlist;

	public $parameterfile;

	private static $logger=false;

	/**
	 *Konstruktor
	 * Initialisieren
	 *@returns $this
	 */
	public function DB()
	{
		$this->lastSQL="";
		$this->insertId=0;
		$this->result=false;

		$this->parameterfile="default";
	}


	public function __destruct()
	{
		//CmyDB::$logger->save2File();
	}

	private function connect($host,$user,$passwd,$parameterfile,$new=false)
	{
		if (!isset(DB::$my_connect[$parameterfile]))
		{
			$con = mysql_connect($host,$user,$passwd,true);
			DB::$my_connect[$parameterfile] = $con;
		}
		else
		{
			$con=DB::$my_connect[$parameterfile];
		}
		if ($con===false)
		{
			echo "Database connection error";
		}
	}


	public function connectFromParams($parameterfile="",$new=false)
	{
		if ($parameterfile=="")
		{
			$parameterfile=$this->parameterfile;
		}
		else
		{
			$this->parameterfile=$parameterfile;
		}
		global $dbparameter;

		$this->connect($dbparameter[$parameterfile]['host'],$dbparameter[$parameterfile]['user'],$dbparameter[$parameterfile]['passwd'],$parameterfile,$new);
		$this->query("USE " . $dbparameter[$parameterfile]['dbname']);
	}


	public function disconnect()
	{
		if (DB::$my_connect[$this->parameterfile])
		{
			$con=mysql_close(DB::$my_connect[$this->parameterfile]);
			unset(DB::$my_connect[$this->parameterfile]);
		}
	}


	public function startTransaction()
	{
		$this->query("START TRANSACTION");
	}


	public function doCommit()
	{
		$this->query("COMMIT");
	}


	public function doRollback()
	{
		$this->query("ROLLBACK");
	}


	public function query($sql)
	{
		$this->lastSQL = $sql;
		$this->result = mysql_query($sql, DB::$my_connect[$this->parameterfile]);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);

		if ($this->error!=0) echo "SQLError (query): " . $this->error  . mysql_error(DB::$my_connect[$this->parameterfile]) . ":: $sql";

	}

	public function insertId()
	{
		$id = mysql_insert_id(DB::$my_connect[$this->parameterfile]);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (insertId): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $id;
	}

	public function fetchArray()
	{
		$fetchArray = mysql_fetch_array($this->result, MYSQL_ASSOC);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (fetchArray): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		if ($fetchArray===false) $fetchArray=array();
		return $fetchArray;
	}
	
	public function fetchObject()
	{
		$fetchObject = mysql_fetch_object($this->result);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (fetchObject): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $fetchObject;
	}
	
	public function fetchRow()
	{
		$fetchRow = mysql_fetch_row($this->result);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (fetchRow): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $fetchRow;
	}


	public function fetch2Array()
	{
		$tuples = $this->numRows();
		$fetch=array();
		if ($tuples!=0)
		{
			for ($i=0; $i<$tuples; $i++)
			{
				$fetch[] = $this->fetchArray();
			}
		}
		return $fetch;
	}


	public function getError()
	{
		return mysql_error(DB::$my_connect[$this->parameterfile]);
	}

	public function getErrorId()
	{
		return $this->error;
	}

	public function affectedRows()
	{
		$tuples = mysql_affected_rows(DB::$my_connect[$this->parameterfile]);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (affectedRows): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $tuples;
	}

	public function numRows()
	{
		$tuples = mysql_num_rows($this->result);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (numRows): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $tuples;
	}
	
	public function numFields()
	{
		$tuples = mysql_num_fields($this->result);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (numFields): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $tuples;
	}


	public function escapeString($string)
	{
		$this->error = 0 ;
		$escapeString = mysql_real_escape_string($string,DB::$my_connect[$this->parameterfile]);
		return $escapeString;
	}


	public function escapeArray($arr)
	{
		$newarr=array();
		if (sizeof($arr)>0)
		{
			foreach($arr as $key => $wert)
			{
				$newarr[$key]=$this->escapeString($wert);
			}
		} else
		{

		}
		return $newarr;

	}
	
	public function listTables($dbname)
	{
		$res = @mysql_list_tables($dbname,DB::$my_connect[$this->parameterfile]);
		$this->error = mysql_errno(DB::$my_connect[$this->parameterfile]);
		if ($this->error!=0) echo "SQLError (listTables): " . $this->error . mysql_error(DB::$my_connect[$this->parameterfile]);
		return $res;
	}
	

};


/*
 //Test
 $dbtest=new DB();

 $dbtest->connectFromParams();

 $SQL="SELECT * FROM nav_user;";

 $dbtest->query($SQL);

 $count=$dbtest->numRows();

 echo "<pre>";
 for($x=0;$x<$count;$x++)
 {
 $zeile=$dbtest->fetchArray();
 print_r($count);
 }
 echo "</pre>";
 */


?>